'''
A set of rules to assure the proper use of assertion's functionality
'''

from string import Template
from rsa.report import ReportManager
from rsa.rule import TemplateRule, RuleViolation

name = "Assertions"

description = __doc__

report = ReportManager()

class BuiltInAssertions(TemplateRule):
    """
    This rule verifies that the Keyword built in assertion keywords are properly used  
    """
    
    rule_id = "BuiltInAssertions"
    name = "Built in assertion"
    message = Template("$item1")
    solution = Template("$item2")
    example = '''
            # Its recommended to add a error message
            Should Be True | ${newRateRow}
    '''
    severity = 2
    description = "BuiltInAssertions find instances where built in assertion keywords are badly used"
    solutions = ""     
           
    def visit_step(self, node):
        if(node.keyword and "Should" in node.keyword):
            self.check_keyword(node, "Should Be True", 2)
            self.check_keyword(node, "Should Be Equal", 3)
            self.check_keyword(node, "Should Contain", 3)
        
    def check_keyword(self, node, keyword, arguments):
        if(node.keyword == keyword):
            if(len(node.args) == arguments):
                if("@{" in node.args[0]):
                    args = ["Argument " + node.args[0] + " shouldn't be a list","Remove " + node.args[0]]
                    report.add_rule_violation(RuleViolation(self, node, args, self.node, self.line))
            else:
                args = ["No error message was found", "Add an error message"]
                report.add_rule_violation(RuleViolation(self, node, args, self.node, self.line))
                
    def visit_loop(self, node):
        for step in node.steps:
            self.line += 1
            self.visit(step)